﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Shared;
using WD = DocumentFormat.OpenXml.Lite.WordprocessingML.Drawing;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using DML = DocumentFormat.OpenXml.Lite.DrawingML;
using DMLG = DocumentFormat.OpenXml.Lite.DrawingML.Geometry;
using DMLM = DocumentFormat.OpenXml.Lite.DrawingML.Media;
using DMLT = DocumentFormat.OpenXml.Lite.DrawingML.Text;
using DMLV = DocumentFormat.OpenXml.Lite.DrawingML.Visual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Lite.WordprocessingML;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunElements;

namespace Samples.Word
{
    class Images
    {
        public static void Create()
        {
            Document doc = DocumentManager.Instance.CreateWordDocument();

            doc.CreateBody().AddParagraph().AddRun().AddSimpleText("Feel the force: OOXML is fun!");

            doc.CreateBody().AddParagraph().AddRun().AddElement(new Break());
            
            Image image;
            using (var ms = new System.IO.MemoryStream())
            {
                Resources.force.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                image = new Image(ms.GetBuffer(), ImageType.JPG);
            }
            string relationshipId = doc.AddPicture(image);

            WD.Drawing drawing = new WD.Drawing();
            drawing.Anchor = new WD.DrawingAnchor()
            {
                DistanceOnTop = 0,
                DistanceOnBottom = 0,
                DistanceOnLeft = 0,
                DistanceOnRight = 0,
                SimplePosition = false,
                RelativeHeight = 5200000,
                BehindDocument = false,
                Locked = false,
                LayoutInCell = true,
                AllowOverlap = true,
                Position = new WD.SimplePosition() { X = 0, Y = 0 },
                HorizontalPositioning = new WD.HorizontalPositioning()
                {
                    RelativeFrom = ST_RelFromH.PageMargin,
                    Element = new WD.HorizontalAlign() { Value = ST_Align.Center }
                },
                VerticalPosition = new WD.VerticalPositioning()
                {
                    RelativeFrom = ST_RelFromV.Line,
                    Element = new WD.AbsolutePositioningOffset() { Value = 0 }
                },
                Extents = new WD.Extent()
                {
                    ExtentLength = 4200000,
                    ExtentWidth = 5200000
                },
                EffectExtents = new WD.EffectExtent()
                {
                    BottomEdge = 0,
                    LeftEdge = 0,
                    RightEdge = 0,
                    TopEdge = 0
                },
                WrapType = new WD.WrapTopAndBottom(),
                Properties = new WD.DrawingObjectProperties()
                {
                    Name = "Picture1",
                    Id = (uint)new Random().Next(100000)
                },
                GraphicProperties = new WD.GraphicFrameProperties(),
                Graphic = new WD.Graphic()
                {
                    Data = new WD.GraphicData()
                    {
                        Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture",
                        DrawingElement = new DMLM.Picture()
                        {
                            NonVisualProperties = new DMLM.Properties.NonVisualPictureProperties()
                            {
                                NonVisualProperties = new DMLG.Properties.NonVisualProperties()
                                {
                                    Id = 1,
                                    Name = "Picture2",
                                    Description = "Image+" + relationshipId,
                                    Hidden = false,
                                },
                                NonVisualPictureDrawingProperties = new DMLG.Properties.NonVisualPictureDrawingProperties()
                            },
                            BlipFill = new DMLV.BlipFill()
                            {
                                Blip = new DMLT.Blip()
                                {
                                    EmbeddedPictureReference = relationshipId
                                },
                                SourceRectangle = new DMLG.Rectangle(),
                                Element = new DMLV.Stretch()
                                {
                                    FillRectangle = new DMLG.Rectangle() { }
                                }
                            },
                            ShapeProperties = new DMLG.Properties.ShapeProperties()
                            {
                                Transform2d = new DML.Transformation.Transform2d()
                                {
                                    Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                    Extents = new DMLG.Extent() 
                                    {
                                        ExtentLength = 420000, 
                                        ExtentWidth = 520000 
                                    }
                                },
                                FillProperty = new DMLV.NoFill(),
                                Geometry = new DML.Transformation.PresetGeometry()
                                {
                                    ShapeType = DML.Types.ST_ShapeType.Rectangle,
                                    AdjustValues = new DMLG.AdjustValueList()
                                },
                                Line = new DMLG.Line()
                                {
                                    LineFillProperty = new DMLV.NoFill()
                                }
                            }
                        }
                    }
                }
            };

            doc.AddNamespace(Constants.GetDefaultPrefix(Constants.Drawing), Constants.Drawing);
            doc.AddNamespace(Constants.GetDefaultPrefix(Constants.Graphic), Constants.Graphic);
            doc.AddNamespace(DML.Constants.GetDefaultPrefix(DML.Constants.Picture), DML.Constants.Picture);
            doc.AddNamespace(Constants.GetDefaultPrefix(Constants.Relationship), Constants.Relationship);

            doc.Body.AddParagraph().AddRun().AddElement(drawing);

            doc.Save(@"samples\image.docx");

            Document parsed = null;
            if (DocumentManager.Instance.OpenWordDocument(@"samples\image.docx", out parsed))
            {
                Image getBack = parsed.FindElement<Image>(relationshipId);
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
